Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor EclipseCollectionsCodeGenerator #1322

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sullis
Copy link
Contributor

@sullis sullis commented Apr 16, 2022

This PR refactors EclipseCollectionsCodeGenerator and adds a new unit test (EclipseCollectionsCodeGeneratorTest).

EclipseCollectionsCodeGeneratorTest validates the generated Java source files for:

  • api
  • impl
  • test

Classgraph library

This PR uses the classgraph library to load resources from the classpath.

When I started this PR, I tried casting the system class loader to URLClassLoader. This approach worked on Java 8 but did not work on later versions of Java.

Reference: https://stackoverflow.com/questions/54618721/migration-to-java11-classcastexception

skipBoolean flag

As I was writing the unit test, I learned that some of the STG templates do not produce valid Java code. I observed that "boolean" is not handled by the templates. I added the skipBoolean flag to these templates:

skipBoolean() ::= "true"

@donraab donraab requested a review from motlin April 18, 2022 01:17
@motlin
Copy link
Contributor

motlin commented Apr 18, 2022

I wasn't familiar with the ClassGraph library before. I like how using it makes it possible to delete so much code from FileUtils. To me, it seems like introducing ClassGraph is the essence of this change. In addition, there are more changes to production code and templates. However, the PR and commit message only mentions adding unit tests.

@sullis sullis changed the title add unit test for EclipseCollectionsCodeGenerator refactor EclipseCollectionsCodeGenerator Apr 18, 2022
@sullis
Copy link
Contributor Author

sullis commented Apr 18, 2022

I wasn't familiar with the ClassGraph library before. I like how using it makes it possible to delete so much code from FileUtils. To me, it seems like introducing ClassGraph is the essence of this change. In addition, there are more changes to production code and templates. However, the PR and commit message only mentions adding unit tests.

I have updated the PR title, PR description, and commit message. The description now contains a better explanation of my work.

1) simplify EclipseCollectionsCodeGenerator constructor parameters
2) add unit test for EclipseCollectionsCodeGenerator
3) verify that all templates produce valid Java source code
4) verify the generated file count
@motlin
Copy link
Contributor

motlin commented Apr 19, 2022

EclipseCollectionsCodeGenerator.generate() delegates to EclipseCollectionsCodeGenerator.sourceFileExists(outputFile) before rendering a file. So if a hand-written boolean version exists, we won't generate code for the template anyway.

I read the test and now understand why you have this. The test just attempts to generate everything. It executes in the code generator module, so we're not even up to compiling the hand-written files yet.

public void validateTestTemplates() throws Exception
{
validateTemplates("test", 2342);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to assert an exact expected number of files, or people will be forced to change it all the time. Asserting it's not an empty collection would be fine IMO.

private static void validateTemplates(String templateDirectory, int expectedFileCount) throws Exception
{
BasicErrorListener errorListener = new BasicErrorListener();
File outputDirectory = Files.createTempDirectory("temp-").toFile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use JUnit's TemporaryFolder rule so that the files are deleted during tearDown of the test rather than the JVM.

ParseResult<CompilationUnit> parseResult = parser.parse(code);
assertTrue(String.valueOf(parseResult.getProblems()) + "\n" + code,
parseResult.isSuccessful());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's kind of cool to have a test assertion like this that asserts a string is valid Java code. But I don't think we really need it either. Our builds generate code and compile it with javac from Java 8, so if this assertion were to fail our build would already be broken. Thoughts?

There are other assertions I could imagine that would be pretty interesting. Are there any places we declare skipBoolean = true but the generated source code matches the hand-written source code? So we don't need to skip boolean generation?

@motlin
Copy link
Contributor

motlin commented Apr 19, 2022

My overall impression is that I like Classgraph and how it helps reduce code. I'm not sure about the test because it's going to run slowly and doesn't seem like it will catch much that the build doesn't already catch. And then there are some refactorings that are only necessary to support the test. I'm curious if anyone else wants to weigh in @donraab @prathasirisha etc

@donraab
Copy link
Contributor

donraab commented Sep 4, 2022

@motlin @sullis I think it makes sense to break this into separate PRs. The skipBoolean change looks straightforward and we should review that separately. If this means we are removing currently generated classes/interfaces then we'll have to think about this, but since 12.0 is a major release I don't have as many concerns about this. On the Classgraph front, looks like it should be ok, but would ask @nikhilnanivadekar if he thinks this will need Eclipse Foundation approval. It's a build time dependency only, and MIT license, so should be ok I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants